package agency.web.app.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import agency.web.app.domain.Country;

import agency.web.app.domain.Tour;

import agency.web.app.repository.TourDao;

import agency.web.app.service.TourService;

@Service
@Transactional
public class TourServiceImpl  implements TourService{

	@Autowired
	private TourDao tourRepository;

	public void addTour(Tour t) {
		tourRepository.add(t);
		
	}

	public void modifyTour(Tour t) {
		tourRepository.modify(t);
		
	}

	public List<Tour> findAllTours() {
		return tourRepository.findAll();
	}

	public Tour findTourById(int id) {
		return tourRepository.findById(id);
	}

	public List<Tour> findTourByCountry(Country country) {
		Criterion criterion = Restrictions.eq("countryName",country.getCountryName());
		return tourRepository.findByCriteria(criterion);
	}

	

	public List<Tour> findTourByPrice(int min, int max) {
		Criterion criterion = Restrictions.between("pricePerOne", min, max);
		return tourRepository.findByCriteria(criterion);
	}

	public List<Tour> findHotTours() {
		List<Tour> tours = tourRepository.findAll();
				for (Tour t : tours) {

			if (!t.isHotTour())
				tours.remove(t);
		}
		return tours;
	}

	public void deleteTourById(int id) {
		tourRepository.delete(id);
	}

	public void deleteTour(Tour c) {
		tourRepository.delete(c);
		
	}



	public List<Tour> findTourByType(String type) {
		Criterion criterion = Restrictions.eq("tourType",type);
		List<Tour> tours = tourRepository.findByCriteria(criterion);
		List<Tour> list = new ArrayList<Tour>();
		for (Tour t : tours) {
			if (t.getType() == type)
				list.add(t);
		}
		return list;
	}	
	

}
